﻿using System;
using System.Collections.Generic;
using System.Text;
using SvnLogParser.Localization;
using SvnLogParser.Utils;

namespace SvnLogParser
{
	public class RevisionInfo
	{
		// private fields
		private readonly StringBuilder _comment = new StringBuilder();
		private Lazy<string> comment_lazy;

		// public fields / properties
		public int? revisionNum;
		public string author;
		public string comment
		{
			get { return comment_lazy.Value; }
		}
		public DateTime date;
		public List<int> IssuesId = new List<int>();
		public List<FileChange> filesChanged = new List<FileChange>();
		public List<int> parentsRevisionsNums = new List<int>(); // main revision(s) number(s) if this is a merged revision (note that a revision can be merge multiple times)
		public bool reverseMerge { get; set; }

		public RevisionInfo()
		{
			comment_lazy = new Lazy<string>(() => _comment.ToString());
			reverseMerge = false;
		}

		public override string ToString()
		{
			StringBuilder sb = new StringBuilder();
			sb.AppendFormat("{0} : {1}", strings.w_revision.FirstLetterUpperCase(), revisionNum.HasValue ? revisionNum.Value.ToString() : "null");
			sb.AppendLine();
			sb.AppendFormat("{0} : {1}", strings.w_author.FirstLetterUpperCase(), author != null ? author : "null");
			sb.AppendLine();
			sb.AppendFormat("{0} : {1:d} {1:t}", strings.w_date.FirstLetterUpperCase(), date);
			sb.AppendLine();
			if (IssuesId.Count > 0) {
				sb.AppendLine(strings.SvnLogIssuesIdHeader);
				foreach (int issueId in IssuesId) {
					sb.AppendFormat("\t#{0}", issueId).AppendLine();
				}
			}
			sb.AppendLine(strings.SvnLogFilesChangedHeader);
			if (filesChanged.Count > 0) {
				foreach (FileChange fc in filesChanged) {
					sb.AppendFormat("\t{0}", fc).AppendLine();
				}
			} else {
				sb.AppendLine("\t" + strings.SvnLogNoFilesChanged);
			}
			if (parentsRevisionsNums.Count > 0) {
				sb.AppendLine(strings.ParentRevisionHeader + " r" + parentsRevisionsNums.GetCollectionAsString<int>(", r"));
			}
			sb.AppendLine(strings.w_comment.FirstLetterUpperCase() + " : ");
			sb.AppendLine(comment);
			sb.Append(new String('-', 50));
			return sb.ToString();
		}

		internal void AddCommentLine( string line )
		{
			// avoid adding a false EOL a the end of StringBuilder
			if (_comment.Length > 0) {
				_comment.AppendLine();
			}
			_comment.Append(line);
		}
	}
}
